var qestore=[];
var qeicons=[];
var qeauth='';

function createXMLRequest()
{
if( window.XMLHttpRequest )
{
return new XMLHttpRequest();
}
else if(window.ActiveXObject)
{
try
{
return new ActiveXObject("Msxml2.XMLHTTP");
}
catch( e )
{
try
{
return new ActiveXObject("Microsoft.XMLHTTP");
}
catch( e )
{
return false;
}
}
}
}


function encq(d)
{
return encodeURIComponent?encodeURIComponent(d):escape(d)
}

function qea(fid,tid,pid,buttonObj)
{
if(qestore[pid])return;

var pref=null;
var lnks=document.anchors;
for(var i=0;i<lnks.length;i++)
{
if(lnks[i].getAttribute('name')=='entry'+pid)
{
pref=lnks[i];
break
}
}

var parearef=pref.nextSibling;
if(parearef.nodeType!=1)parearef=parearef.nextSibling;
parearef=parearef.getElementsByTagName('td')[3];
parearef.id="posta"+pid;

qestore[pid]=parearef.innerHTML;
parearef.innerHTML='Warning: Using this will destroy any non ASCII latin characters<br /><textarea id="postc'+pid+'" disabled="disabled" style="width: 100%; height: 200px">Please wait as the post loads...</textarea>'+
'<br /><input id="qesb'+pid+'" type="button" value="Save" disabled="disabled" onclick="qed('+fid+','+tid+','+pid+')" /> <input id="qecb'+pid+'" type="button" disabled="disabled" value="Cancel" onclick="qec('+pid+')" />';

var thd=createXMLRequest();
thd.open('GET','index.php?act=Post&CODE=08&f='+fid+'&t='+tid+'&p='+pid,true);
thd.onreadystatechange = function(){if(thd.readyState==4){qes(fid,tid,pid,thd)}};
thd.send(null);
}

function qes(fid,tid,pid,thd)
{
var carea=document.getElementById('postc'+pid);
var c=thd.responseText.match(/<textarea cols='80' rows='20' name='Post' tabindex='3' class='textinput'>((.|\n)*)<\/textarea>/)[1];
if(!c)c=thd.responseText.match(/<textarea cols='80' rows='20' name='Post' tabindex='3' class='textinput'>(.*)<\/textarea>/)[1];
qeauth=thd.responseText.match(/auth_key' value='(.{32})'/)[1];
qeicons[pid]=thd.responseText.match(/name=('|")iconid('|") value=('|")([0-9]+)('|") checked/)[4];
var ta=document.forms['temp'];
ta.innerHTML='<textarea name="temptext">'+c+'</textarea>';
carea.value=ta.temptext.value;
ta.innerHTML='';
carea.disabled=false;
document.getElementById('qesb'+pid).disabled=false;
document.getElementById('qecb'+pid).disabled=false;
}

function qec(pid)
{
pa=document.getElementById('posta'+pid);
pa.innerHTML=qestore[pid];
qestore[pid]=null;
}

function qed(fid,tid,pid)
{
pa=document.getElementById('postc'+pid);
if(pa.value==''){alert('Please enter a post!');return}
document.getElementById('qesb'+pid).value="Saving... please wait";
var thd=createXMLRequest();
thd.open('POST','index.php?act=Post&CODE=09&f='+fid+'&t='+tid+'&p='+pid+'&auth_key='+qeauth,true);
thd.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
thd.onreadystatechange = function(){if(thd.readyState==4){qel(tid,pid)}};
thd.send('editupload=keep&add_edit=1&iconid='+qeicons[pid]+'&enableemo=yes&enablesig=yes&Post='+encq(pa.value));
}

function qel(tid,pid)
{
var thd=createXMLRequest();
thd.open('GET','index.php?showtopic='+tid+'&view=findpost&p='+pid,true);
thd.onreadystatechange = function(){
if(thd.readyState==4){
var r=new RegExp('<!\-\- THE POST '+pid+' \-\->((.|\n)*?)<!\-\- THE POST \-\->');
var c=r.exec(thd.responseText)[1];
var pa=document.getElementById('posta'+pid);
pa.innerHTML=c;
qestore[pid]=null;
}
};
thd.send(null);
}

function qesetup()
{
if(!createXMLRequest)return;
if(!document.getElementById)return;
if(!document.body.innerHTML)return;
var lnks=document.links;
for(var i=0;i<lnks.length;i++)
{
if(lnks[i].href.match(/act=Post&CODE=08/))
{
var fid=lnks[i].href.match(/f=([0-9]+)/)[1];
var tid=lnks[i].href.match(/t=([0-9]+)/)[1];
var pid=lnks[i].href.match(/p=([0-9]+)/)[1];
var b=document.createElement('span');
b.innerHTML='<a style="cursor:pointer" onclick="qea('+fid+','+tid+','+pid+',this)">'+qebutton+'</a>';
lnks[i].parentNode.insertBefore(b,lnks[i]);
}
}

document.write('<form name="temp" id="temp" style="display:none"></form>');
}